package 多线程初阶.示例测试;

/**
 * 中断一个线程:使用自定义的变量来作为标志位.
 */
public class TestDemo4 {
   private static class MyRunnable implements Runnable{
        public volatile boolean isQuit = false;//这是标志位
        @Override
        public void run() {
            while (!isQuit){
                System.out.println(Thread.currentThread().getName()+" is run");
                try {
                    Thread.sleep(1*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+" will die");
        }
    }

    public static void main(String[] args) throws InterruptedException {
       MyRunnable target=new MyRunnable();
       Thread thread=new Thread(target,"MyThread");
        System.out.println(Thread.currentThread().getName()+" is run");
        thread.start();
        Thread.sleep(10*1000);
        System.out.println(Thread.currentThread().getName()+" receive notice");
        target.isQuit=true;//中断
    }
}
